###############################################################################
########08. Summary Statistics##########################
###############################################################################

pols <- readRDS('pols_bjps')

####Table 4: All Possible Cases and Dependent Variable#####
#install.packages('dplyr')
library(dplyr)
#install.packages('ggplot2)
library(ggplot2)

pols$electionyear <- as.numeric(pols$electionyear)

table.data <- pols[which(complete.cases(pols$econsdw, pols$gcov_inc1, pols$socioeconomicsal, pols$gini_disp, pols$enep, pols$galtansdw, pols$scale_mag,pols$eastwest, pols$legislative_type, pols$electionyear, pols$country.name, pols$unemp)),]

table1 <- table.data %>%
  dplyr::group_by(country.name) %>%
  dplyr::summarize(elections = n(),
                   earliest_year = min(electionyear),
                   latest_year = max(electionyear),
                   mean_econ = mean(econsdw, na.rm = T),
                   min_econ = min(econsdw, na.rm = T),
                   max_econ = max(econsdw, na.rm = T))

print(xtable::xtable(table1, digits = 3))

#####Table 5: GGINI and overall inequality#####

table2 <- table.data %>%
  dplyr::group_by(country.name) %>%
  dplyr::summarize(meanhorinc = mean(scale_ggini_inc1, na.rm = T),
                   minhorinc = min(scale_ggini_inc1,na.rm = T),
                   maxhorinc = max(scale_ggini_inc1,na.rm = T),
                   meanpostfisc = mean(gini_disp, na.rm = T),
                   minpostfisc = min(gini_disp, na.rm = T),
                   maxpostfisc = max(gini_disp, na.rm = T),
                   meangaltan = mean(galtansdw, na.rm = T),
                   mingaltan = min(galtansdw, na.rm = T),
                   maxgaltan = max(galtansdw, na.rm = T))
print(xtable::xtable(table2, digits = 3))

#####Table 6: Salience and continuous controls#####

table3 <- table.data%>%
  dplyr::group_by(country.name) %>%
  dplyr::summarize(meaneconsal = mean(socioeconomicsal, na.rm = T),
                   mineconsal = min(socioeconomicsal, na.rm = T),
                   maxeconsal = max(socioeconomicsal, na.rm = T),
                   meanenep = mean(enep, na.rm = T),
                   minenep = min(enep, na.rm = T),
                   maxenep = max(enep, na.rm = T),
                   meanmag = mean(tier1_avemag, na.rm = T),
                   minmag = min(tier1_avemag, na.rm = T),
                   maxmag = max(tier1_avemag, na.rm = T),
                   meanunemp = mean(unemp, na.rm = T),
                   minunemp = min(unemp, na.rm = T),
                   maxunemp = max(unemp, na.rm = T))
print(xtable::xtable(table3, digits = 3))

#####Table 7: Correlation Matrix of Variables in Main Models#####

#Load Necessary Package#
#install.packages('ggcorrplot')
library(ggcorrplot)


#Transform data and rename varaibles#
corplot <- as.data.frame(pols) %>%
  dplyr::select(scale_econsdw, 
                scale_ggini_inc1,
                scale_ggini_inc2,
                scale_ggini_inc3,
                scale_ggini_inc4,
                scale_ggini_inc5,
                scale_socioeconomicsal,
                scale_dispgini,
                scale_enep,
                scale_galtansdw,
                scale_mag,
                scale_unemp) %>%
  dplyr::rename(Econ_Polarization = scale_econsdw,
         Income_Dif1 = scale_ggini_inc1,
         Income_Dif2 = scale_ggini_inc2,
         Income_Dif3 = scale_ggini_inc3,
         Income_Dif4 = scale_ggini_inc4,
         Income_Dif5 = scale_ggini_inc5,
         Econ_Salience = scale_socioeconomicsal,
         Disp_Gini = scale_dispgini,
         ENEP = scale_enep,
         GALTAN_Polarization = scale_galtansdw,
         Avg_Magnitude = scale_mag,
         Unemployment = scale_unemp)%>%
  na.omit %>%
  select(-Avg_Magnitude)

#Create data for correlations#
corr <- round(cor(corplot), 3)

#Create p-values#
p.mat <- cor_pmat(corplot)

print(xtable::xtable(corr, digits = 3))

print(xtable::xtable(p.mat, digits = 3))

